﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormOrderPayment : FormTemplet
    {
        private FormOrderPayment()
        {
            this.InitializeComponent();
        }

        public FormOrderPayment(string sOrderCode) : this()
        {
            this.txtOrderCode.Text = sOrderCode;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.cbPaymentName.Text.Trim().Length > 32 || this.cbPaymentName.Text.Trim().Length < 1)
            {
                MessageBox.Show("款项 格式不正确，字符不能为空 且 小于等于32个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.cbPaymentName.Focus();
            }
            else if (!Regex.IsMatch(this.txtAmount.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,4}))$"))
            {
                MessageBox.Show("金额 格式不正确，此处应该为不为零的整数，如有小数，最大为4位小数", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtAmount.Focus();
            }
            else if (this.cbWayOfPayment.Text.Trim().Length > 16 || this.cbWayOfPayment.Text.Trim().Length < 1)
            {
                MessageBox.Show("付款方式 格式不正确，字符不能为空 且 小于等于16个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.cbWayOfPayment.Focus();
            }
            else if (this.txtPaymentAddress.Text.Trim().Length > 64)
            {
                MessageBox.Show("付款地址 格式不正确，应小于等于64个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtPaymentAddress.Focus();
            }
            else if (this.txtRemark.Text.Trim().Length > 64)
            {
                MessageBox.Show("付款地址 格式不正确，应小于等于64个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtRemark.Focus();
            }
            else
            {
                Orders orders = new Orders();
                bool flag = false;
                try
                {
                    flag = orders.AddPayment(this.txtOrderCode.Text.Trim(), this.dtpPaymentDate.Value, this.cbPaymentName.Text.Trim(), Convert.ToDouble(this.txtAmount.Text.Trim()), this.cbWayOfPayment.Text.Trim(), this.txtPaymentAddress.Text.Trim(), this.txtRemark.Text.Trim(), LoginInfo.OperatorID);
                }
                catch (SqlException ex)
                {
                    if (ex.Number < 50000)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orders.Dispose();
                if (flag)
                {
                    MessageBox.Show("录入成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    base.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void FormOrderPayment_Activated(object sender, EventArgs e)
        {
            base.WindowState = FormWindowState.Normal;
        }
    }
}
